extends Panel

signal new_name

onready var name_input = get_node('vbox/name_input')
onready var row1 = get_node('vbox/row1')
onready var row2 = get_node('vbox/row2')
onready var row3 = get_node('vbox/row3')
onready var t1 = row1.get_node('c1')
onready var t2 = row1.get_node('c2')
onready var t3 = row1.get_node('c3')
onready var t4 = row2.get_node('c1')
onready var t5 = row2.get_node('c2')
onready var t6 = row2.get_node('c3')
onready var t7 = row3.get_node('c1')
onready var t8 = row3.get_node('c2')
onready var t9 = row3.get_node('c3')

func _ready():
	pass

func show_name(start_name, tags):
	name_input.grab_focus()
	name_input.set_text(start_name)
	set_tags(tags)
	show()

func on_btn_ok_pressed():
	close()
	var tags = get_tags()
	var name = name_input.get_text().strip_edges()
	if name != '':
		emit_signal('new_name', name, tags)

func close():
	hide()

func get_tags():
	var tags = StringArray()
	if t1.is_pressed():
		tags.push_back(t1.get_text())
	if t2.is_pressed():
		tags.push_back(t2.get_text())
	if t3.is_pressed():
		tags.push_back(t3.get_text())
	if t4.is_pressed():
		tags.push_back(t4.get_text())
	if t5.is_pressed():
		tags.push_back(t5.get_text())
	if t6.is_pressed():
		tags.push_back(t6.get_text())
	if t7.is_pressed():
		tags.push_back(t7.get_text())
	if t8.is_pressed():
		tags.push_back(t8.get_text())
	if t9.is_pressed():
		tags.push_back(t9.get_text())

	return tags

func set_tags(tags):
	t1.set_pressed(has_tag(tags, t1.get_text()))
	t2.set_pressed(has_tag(tags, t2.get_text()))
	t3.set_pressed(has_tag(tags, t3.get_text()))
	t4.set_pressed(has_tag(tags, t4.get_text()))
	t5.set_pressed(has_tag(tags, t5.get_text()))
	t6.set_pressed(has_tag(tags, t6.get_text()))
	t7.set_pressed(has_tag(tags, t7.get_text()))
	t8.set_pressed(has_tag(tags, t8.get_text()))
	t9.set_pressed(has_tag(tags, t9.get_text()))

func has_tag(tags, tag):
	for idx in range(tags.size()):
		if tags[idx] == tag:
			return true
	return false
